package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.pojo.Category;
import com.itheima.pojo.Employee;
import com.itheima.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YangJiaHao
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public CommonResult save(@RequestBody Category category){
        categoryService.save(category);
        return CommonResult.success("添加成功");
    }

    @GetMapping("/list")
    public CommonResult list(Integer type){
        List<Category> list = categoryService.getList(type);
        return CommonResult.success(list);
    }

    @GetMapping("/page")
    public CommonResult page(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Category> pageInfo = categoryService.page(page, pageSize);
        return CommonResult.success(pageInfo);
    }

    @DeleteMapping
    public CommonResult delete(String id){
        categoryService.delete(id);
        return CommonResult.success("删除成功");
    }

    @PutMapping
    public CommonResult update(@RequestBody Category category){
        categoryService.update(category);
        return CommonResult.success("修改成功");
    }
}
